// Copyright (c) WanSheng Intelligent Corp. All rights reserved.
// Licensed under the LGPL v3.0 license. 
using namespace std;
#ifndef __SNAP7_PLUGIN_H__
#define __SNAP7_PLUGIN_H__

#define LOG_TAG "SNAP7"

#include <stdlib.h>
#include "snap7.h"
#include <linux/prctl.h>
#include "path.h"
#include "ams_constants.h"

#include "plugin_constants.h"
#include "agent_core_lib.h"
#include "parson.h"
#include "er-coap.h"
#include "er-coap-constants.h"
#include "string_parser.h"
#include "logs.h"
#include "parson_ext.h"
#include "modbus_coding.h"
#include "cJSON_ext.h"
#include "wa_edge_types.h"
#include "wa_plugin_sdk.h"
#include "wa_bus.h"

#include <string>

#define PLUGIN_NAME "snap7_plugin"
#define MAX_S7_DEVICE 50
//#define FLAG_MODBUS_ACCESS  		0x00000001
//#define FLAG_MODBUS_LOG				0x00000002
//#define FLAG_RESOURCE_WRITE			0x00000004
//#define FLAG_RESOURCE_READ			0x00000008
//#define FLAG_RESOURCE_META			0x00000010
//
//
//
//#define FLAG_MODBUS_SUCCESS 		0x10000000
//#define FLAG_MODBUS_DATA			0x20000000
//#define FLAG_PING					0x40000000
//#define FLAG_BUS_AUTO_CLOSE			0x80000000
//#define FLAG_DEBUG_URL				0x01000000
//#define FLAG_VALUE_CONVERT          0x02000000
//#define FLAG_MB_CMD_ERROR           0x04000000
//#define FLAG_MB_CMD_MSG             0x08000000


//#define SNAP7_LOG_DATA(title, buffer, len) if((log_tag_mask[Log2_Modbus] & FLAG_MODBUS_DATA) && (len > 0) && (LOG_VERBOSE   == log_level)) log_buffer(title,buffer, len)
//#define NOT_DEFINED (-1)
//
//#define ZS_TASK_CONNECT "BUS-CONNECT"

typedef enum
{
	T_Invalid_DataType = -1,
	T_Bits = 0,
	T_int8,
	T_Uint8,
	T_int16,
	T_Uint16,
	T_int32,
	T_Uint32,
	T_Float,
	T_int64,
	T_Uint64,
	T_Double,
} s7_dt_t;

struct s7_device_t
{
	string ip;
	int rack;
	int slot;
	int type;
	wa_plugin_device_info_t* plugin_cfg;
	TS7Client* dc = nullptr;
};

extern s7_device_t *g_s7_devices[MAX_S7_DEVICE];
extern void snap7_Get(restful_request_t* request, REQ_ENV_HANDLE req_env);
extern void snap7_Put(restful_request_t* request, REQ_ENV_HANDLE req_env);
extern bool load_device_config(const char * plugin_name);
extern void open_s7_device(s7_device_t *device);
#endif 
